/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.swta;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author JKorreck
 */
public class MyItemPackageManager {

    private static MyItemPackageManager INSTANCE = new MyItemPackageManager();
    private HashMap<String, MyItemPackage> packages = new HashMap<String, MyItemPackage>();
    private ArrayList<PropertyChangeListener> l = new ArrayList<PropertyChangeListener>();

    private MyItemPackageManager() {
    }

    public static MyItemPackageManager getInstance() {
        return INSTANCE;
    }

    public void addItemSet(MyItemPackage newItemSet) {
        System.out.println("___________BEFORE: ");
        Iterator<String> keyIt = this.packages.keySet().iterator();
        
        while(keyIt.hasNext()) {
            System.out.println("I: " + keyIt.next());
        }

        System.out.println("___________ADD ITEM WITH ID:" + newItemSet.getId());
        if (!this.packages.containsKey(newItemSet.getId())) {
            System.out.println("__________DON'T CONTAINS ADD ITEM WITH ID:" + newItemSet.getId());

            this.packages.put(newItemSet.getId(), newItemSet);
        }

        System.out.println("AFTER: ");
        keyIt = this.packages.keySet().iterator();
        while(keyIt.hasNext()) {
            System.out.println("I: " + keyIt.next());
        }
    }
    
    public void updateItemSet(MyItemPackage oldPackage, MyItemPackage newItemPackage) {
        System.out.println("OLD: " + oldPackage.getId());
        this.packages.remove(oldPackage);
        System.out.println("NEW: " + newItemPackage.getId());
        this.packages.put(newItemPackage.getId(), newItemPackage);
    }


    public void removeItemSet(MyItemPackage newItemSet) {
        this.packages.remove(newItemSet);
    }

    public void removeItemPackage(String id) {
        if (packages.containsKey(id)) {
            this.packages.remove(id);
            this.l.get(0).propertyChange(new PropertyChangeEvent(this, "remove", id, ""));
        }
    }

    public MyItemPackage getItemPackage(String id) {
        if (!this.packages.containsKey(id)) {
            return new MyItemPackage();
        } else {
           return this.packages.get(id);
        }
        
    }

    public boolean hasItemPackage(String itemSetID) {
        return this.packages.containsKey(itemSetID);
    }

    public List<MyItemPackage> getAllItemPackages() {
        return new ArrayList(this.packages.values());
    }

    public int getSize() {
        return this.packages.size();
    }

    public List<MyItemPackage> find(int first, int count, String sortProperty, boolean sortAsc) {

        System.out.println("MNG: " + this.packages.size());
        List<MyItemPackage> subList = getIndex(sortProperty, sortAsc).subList(first, first + count);

        return subList;
    }

    private List<MyItemPackage> getIndex(String sortProperty, boolean sortAsc) {
        // hier müsste die DB gefragt werden und dementsprechend sortiert werden

        if (sortProperty == null) {
            return new ArrayList<MyItemPackage>(this.packages.values());

        } else if (sortProperty.equals("id")) {

            ArrayList<MyItemPackage> tmpItemSet = new ArrayList<MyItemPackage>(this.packages.values());


            return this.sortByID(tmpItemSet, sortAsc);

        } else if (sortProperty.equals("title")) {

            ArrayList<MyItemPackage> tmpItemSet = new ArrayList<MyItemPackage>(this.packages.values());


            return this.sortByTitle(tmpItemSet, sortAsc);

        } else if (sortProperty.equals("packagePrice")) {

            ArrayList<MyItemPackage> tmpItemSet = new ArrayList<MyItemPackage>(this.packages.values());


            return this.sortByPackagePrice(tmpItemSet, sortAsc);

        } else if (sortProperty.equals("discount")) {

            ArrayList<MyItemPackage> tmpItemSet = new ArrayList<MyItemPackage>(this.packages.values());


            return this.sortByDiscount(tmpItemSet, sortAsc);

        }

//       else if (sortProperty.equals("name")) {
//
//           return this.sortByName(new ArrayList<MyItemPackage>(this.allItems.values()), sortAsc);
//
//       } else if (sortProperty.equals("category")) {
//
//           return this.sortByCategory(new ArrayList<MyItem>(this.allItems.values()), sortAsc);
//       }

        throw new RuntimeException("unknown sort option [" + sortProperty +
                "]. valid options: [id] , [name]");
    }

    private List<MyItemPackage> sortByID(ArrayList<MyItemPackage> items, boolean asc) {

        if (asc) {
            Collections.sort(items, new Comparator() {

                public int compare(Object o1, Object o2) {

                    return ((MyItemPackage) o1).getId().compareTo(((MyItemPackage) o2).getId());
                }
            });
        } else {

            Collections.sort(items, new Comparator() {

                public int compare(Object o1, Object o2) {

                    return ((MyItemPackage) o2).getId().compareTo(((MyItemPackage) o1).getId());
                }
            });
        }
        return items;
    }

    private List<MyItemPackage> sortByTitle(ArrayList<MyItemPackage> items, boolean asc) {

        if (asc) {
            Collections.sort(items, new Comparator() {

                public int compare(Object o1, Object o2) {

                    return ((MyItemPackage) o1).getTitle().compareTo(((MyItemPackage) o2).getTitle());
                }
            });
        } else {

            Collections.sort(items, new Comparator() {

                public int compare(Object o1, Object o2) {

                    return ((MyItemPackage) o2).getTitle().compareTo(((MyItemPackage) o1).getTitle());
                }
            });
        }
        return items;
    }

    private List<MyItemPackage> sortByPackagePrice(ArrayList<MyItemPackage> items, boolean asc) {

        if (asc) {
            Collections.sort(items, new Comparator() {

                public int compare(Object o1, Object o2) {

                    int smaller = 0;

                    if (((MyItemPackage) o1).getPackagePrice() < ((MyItemPackage) o2).getPackagePrice()) {
                        smaller = -1;
                    } else if (((MyItemPackage) o1).getPackagePrice() > ((MyItemPackage) o2).getPackagePrice()) {
                        smaller = 1;
                    }
                    return smaller;

                }
            });
        } else {

            Collections.sort(items, new Comparator() {

                public int compare(Object o1, Object o2) {

                    int smaller = 0;

                    if (((MyItemPackage) o2).getPackagePrice() < ((MyItemPackage) o1).getPackagePrice()) {
                        smaller = -1;
                    } else if (((MyItemPackage) o2).getPackagePrice() > ((MyItemPackage) o1).getPackagePrice()) {
                        smaller = 1;
                    }
                    return smaller;

                }
            });
        }
        return items;
    }

    private List<MyItemPackage> sortByDiscount(ArrayList<MyItemPackage> items, boolean asc) {

        if (asc) {
            Collections.sort(items, new Comparator() {

                public int compare(Object o1, Object o2) {

                    int smaller = 0;

                    if (((MyItemPackage) o1).getDiscount() < ((MyItemPackage) o2).getDiscount()) {
                        smaller = -1;
                    } else if (((MyItemPackage) o1).getDiscount() > ((MyItemPackage) o2).getDiscount()) {
                        smaller = 1;
                    }
                    return smaller;

                }
            });
        } else {

            Collections.sort(items, new Comparator() {

                public int compare(Object o1, Object o2) {

                    int smaller = 0;

                    if (((MyItemPackage) o2).getDiscount() < ((MyItemPackage) o1).getDiscount()) {
                        smaller = -1;
                    } else if (((MyItemPackage) o2).getDiscount() > ((MyItemPackage) o1).getDiscount()) {
                        smaller = 1;
                    }
                    return smaller;

                }
            });
        }
        return items;
    }

    public void addPCL(PropertyChangeListener l) {
        this.l.add(l);
    }
}
